package acs.bridge;

import acs.bridge.dao.Bm_jreportDAOImpl;
import acs.bridge.dao.BridgeDAOImpl;
import acs.bridge.jreport.JReportCollection;
import acs.bridge.utility.UtilityParameters;
import org.apache.log4j.Logger;
import org.apache.tools.ant.util.ReaderInputStream;

import java.math.BigDecimal;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.Properties;

/**
 * home
 * ACS
 * User: stas
 * Date: 24.05.13
 * Time: 17:54
 */
public class bm_jreport implements BridgeModuleInterface, BridgeModuleExtInterface {
    static String version = "2";
    Logger logger = Logger.getLogger(this.getClass().getName());

    public static Bm_jreportDAOImpl getBm_jreportDAOImpl() {
        return bm_jreportDAOImpl;
    }

    public void setBm_jreportDAOImpl(Bm_jreportDAOImpl bm_jreportDAOImpl) {
        this.bm_jreportDAOImpl = bm_jreportDAOImpl;
    }

    private static Bm_jreportDAOImpl bm_jreportDAOImpl;

    public static String getModule_cdata_tag() {
        return module_cdata_tag;
    }

    public void setModule_cdata_tag(String jreport_tag) {
        this.module_cdata_tag = jreport_tag;
    }

    /* Тег модуля jreport (bridge_module_cdata.tag)*/
    private static String module_cdata_tag;

    public static String getModule_tag() {
        return module_tag;
    }

    public static void setModule_tag(String module_tag) {
        bm_jreport.module_tag = module_tag;
    }

    private static String module_tag;

    public static BridgeDAOImpl bridgeDAOImpl;

    public static void main(String[] args) {
    }

    public void setBridgeDAOImpl(BridgeDAOImpl bridgeDAOImpl) {
        this.bridgeDAOImpl = bridgeDAOImpl;
    }

    @Override
    public void execute(String a_group_tag, String a_meta_tag, BigDecimal a_raw_id, String a_module_tag) throws BridgeModuleException, BridgeModuleCriticalException {
        logger.info("BridgeModule " + this.getClass().getName() + " ver." + version + " {raw_id=" + a_raw_id + "}");

        setModule_tag(a_module_tag);
        /* Читаем настройки отчета */
        setModule_cdata_tag(bridgeDAOImpl.get_string_field_by_id(a_group_tag, a_meta_tag, a_raw_id, "MODULE_CDATA_TAG", "RAW"));

        logger.info("Get cdata {jreport_tag=" + getModule_cdata_tag() + "}");
        Clob module_cdata = bm_jreportDAOImpl.fnc_get_module_cdata(getModule_tag(), getModule_cdata_tag(), "POM");
        try {

            logger.info("Get context");
            StreamXmlApplicationContext bridge_cdata_context = new StreamXmlApplicationContext(
                    new ReaderInputStream(module_cdata.getCharacterStream(), "UTF-8"));

            logger.info("Get cdata JReportCollection");
            JReportCollection jReportCollection = (JReportCollection) bridge_cdata_context.getBean("JReportCollection");
            Properties parameters = jReportCollection.getParameters();
            parameters = UtilityParameters.combineProperties(parameters, bm_jreportDAOImpl.fnc_get_param_field_by_id(jReportCollection.getJdbcTemplate().getDataSource().getConnection(), a_group_tag, a_meta_tag, a_raw_id, "PARAM_LIST", "RAW"));
            jReportCollection.setParameters(parameters);
            jReportCollection.makeReportCollection();
        } catch (SQLException e) {
            throw new BridgeModuleException("getAsciiStream error.", e);
        }


    }

    @Override
    public void initial_processing() {
    }

    @Override
    public void execute_meta_start(String a_group_tag, String a_meta_tag) {
        logger.info("execute_meta_start");
    }

    @Override
    public void execute_meta_finish(String a_group_tag, String a_meta_tag) {
        logger.info("execute_meta_finish");
    }

    @Override
    public void final_processing(BigDecimal bigDecimal) {
    }

}